package com.winchain.chainbill.asset.biz.credit.rx;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.acct.transaction.vo.AcctInfo;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.shell.stellar.StellarConfiguration;
import com.winchain.chainbill.asset.biz.credit.AssetEvent;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import com.winchain.chainbill.trade.service.TradeService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AssetTranSubscriber extends TxSubscriber<AssetEvent<AcctTxRec>> {
	@Autowired
	private TradeService tradeService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;

	public void onApplicationEvent(AssetEvent<AcctTxRec> t) {
		List<AcctTxRec> records = t.getRecords();

		String txSn = SystemUtil.getUUID();

		List<AcctTxRec> list = new ArrayList<AcctTxRec>();

		String source = null;
		for (Iterator localIterator = records.iterator(); localIterator.hasNext();) {
			AcctTxRec txRec = (AcctTxRec) localIterator.next();
			if (txRec != null) {
				AcctTxRec AcctTxRec = getAcctTxRec(txRec);

				list.add(AcctTxRec);
				AcctTxRec.setTxSn(txSn);
				if (source == null) {
					source = AcctTxRec.getTxPassword();
				}
			}
		}
		Object result = this.tradeService.fire(source, list);
		for (AcctTxRec txRec : list) {
			txRec.setTxSnHost((String) ((Map) result).get("tx_sn"));
			t.setValue("txSnHost", txRec.getTxSnHost());
			if ("S".equals(((Map) result).get("ret_code"))) {
				this.tradeService.callback(txRec);
			} else {
				this.tradeService.rollback(txRec);
			}
		}
	}

	private AcctTxRec getAcctTxRec(AcctTxRec txRec) {
		BigDecimal amt = txRec.getTxAmt();
		String currCode = "";
		String bizType = txRec.getTxType();
		String cusId = CommonDataUtil.getCurEntId();

		String abs = "";
		String acctType = null;
		String creditAcctType = null;
		switch (bizType) {
		case "dist_cred":
			abs = "������������";
			acctType = "9";
			creditAcctType = "1";
			currCode = AssetCode.XQT.name();
			break;
		case "dist_trans":
			abs = "����������-��������";
			acctType = "9";
			creditAcctType = "2";
			currCode = AssetCode.XCB.name();
			break;
		case "adj_cred":
			abs = "������������";
			currCode = AssetCode.XQT.name();
			if (txRec.getOptType().equals("qt_inc")) {
				acctType = "9";
				creditAcctType = "1";
			} else {
				acctType = "1";
				creditAcctType = "9";
			}
			break;
		}
		txRec.setTxId(SystemUtil.getUUID());

		AcctInfo acctInfo = new AcctInfo();
		acctInfo.setCustId(cusId);
		acctInfo.setAcctUse(acctType);

		AcctBas finAcct = null;
		if ("9".equals(acctType)) {
			txRec.setTxAcctNo(StellarConfiguration.getCentralbankAddr());
			txRec.setTxPassword(StellarConfiguration.getCentralbankSeed());

			finAcct = getAcctBas(cusId, creditAcctType, null);
			txRec.setCpAcctNo(finAcct.getAcctNo());
			txRec.setCpAcctNm(finAcct.getAcctNm());
			txRec.setCpBankNm(finAcct.getFiNm());
		} else {
			finAcct = getAcctBas(cusId, acctType, null);
			txRec.setTxAcctNo(finAcct.getAcctNo());
			txRec.setTxAcctNm(finAcct.getAcctNm());
			txRec.setTxBankNm(finAcct.getFiNm());
			txRec.setTxPassword(finAcct.getAcctKey());

			txRec.setCpAcctNo(StellarConfiguration.getCentralbankAddr());
		}
		txRec.setCurrCode(currCode);
		txRec.setTxChnCode("C");

		AcctBkp txAcctBkp = this.billAcctBlcService.getAcctBkp(txRec.getTxAcctNo(), txRec.getCurrCode(), null, null);
		if (txAcctBkp != null) {
			txRec.setTxBkpId(txAcctBkp.getBkpId());
		} else {
			txRec.setTxBkpId("COLD_WALLET");
		}
		AcctBkp cpAcctBkp = this.billAcctBlcService.getAcctBkp(txRec.getCpAcctNo(), txRec.getCurrCode(), null, null);
		if (cpAcctBkp != null) {
			txRec.setCpBkpId(cpAcctBkp.getBkpId());
		} else {
			txRec.setCpBkpId("COLD_WALLET");
		}
		txRec.setTxAmt(amt);
		txRec.setTxDt(SysDateUtil.getSystemDate());
		txRec.setTxTs(SysDateUtil.getSystemDateTime());
		txRec.setTxAbs(abs);
		txRec.setTxOperator(CommonDataUtil.getUserId());

		txRec.setTxSts("I");

		txRec.setTxCashFlag("N");
		txRec.setTxReqFiCls("1");

		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);

		txRec.setDataScLvl("B");
		return txRec;
	}
}
